package com.tudouji.project.customer.controller;

import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户管理Controller
 *
 * @author wf
 * @date 2020-12-30
 */
@RestController
@RequestMapping("/customer/customer")
public class CustomerController extends BaseController
{
    @Autowired
    private ICustomerService customerService;

    /**
     * 查询客户管理列表
     */
    @GetMapping("/wxlist")
    public TableDataInfo wxlist(Customer customer, String status, String functionType) {
//        startPage();
        List<Customer> list = customerService.selectCustomerWxList(customer, status, functionType);
        return getDataTable(list);
    }

    /**
     * 查询客户管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Customer customer)
    {
//        List<Integer> statusList = new ArrayList<Integer>();
//        statusList.add(1);
//        statusList.add(2);
//        customer.setStatusList(statusList);
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 查询客户管理列表
     */
    @GetMapping("/listAmountSum")
    public AjaxResult listAmountSum(Customer customer)
    {
        List<Integer> statusList = new ArrayList<Integer>();
        statusList.add(1);
        statusList.add(2);
        customer.setStatusList(statusList);
        return AjaxResult.success(customerService.selectCustomerListSum(customer));
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Customer customer)
    {
        List<Integer> statusList = new ArrayList<Integer>();
        statusList.add(1);
        statusList.add(2);
        customer.setStatusList(statusList);
        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        return util.exportExcel(list, "customer");
    }

    /**
     * 获取客户管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('customer:customer:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(customerService.selectCustomerById(guid));
    }

    /**
     * 获取客户管理详细信息
     */
    @GetMapping(value = "/detail")
    public AjaxResult detail(Long id)
    {
        return customerService.detail(id);
    }

    /**
     * 新增客户管理
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer)
    {
        //customer.setType(1);
        if(customer.getType().equals("2")){
            Customer c = new Customer();
            c.setOrgguid(customer.getOrgguid());
            List<Customer> customerList = this.customerService.selectCustomerList(c);
            if(customerList.size() > 0){
                return AjaxResult.error("关联组织已经存在，请重新选择关联组织");
            }
        }
        Customer  cu = new Customer();
        cu.setAllname(customer.getName());
        List<Customer> customers = this.customerService.selectCustomerList(cu);
        if(customers.size() > 0){
            return AjaxResult.error("该客户名称已经存在");
        }
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer)
    {
        AjaxResult error = getAjaxResult(customer);
        if (error != null) return error;
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 修改客户管理-Wx
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping("wx")
    public AjaxResult editWx(@RequestBody Customer customer)
    {
        AjaxResult error = getAjaxResult(customer);
        if (error != null) return error;
        return toAjax(customerService.updateCustomer(customer));
    }

    private AjaxResult getAjaxResult(Customer customer) {
        if(customer.getType().equals("2")){
            Customer c = new Customer();
            c.setGuid(customer.getGuid());
            c.setOrgguid(customer.getOrgguid());
            List<Customer> customerList = this.customerService.selectCustomerList(c);
            if(customerList.size() > 0){
                return AjaxResult.error("关联组织已经存在，请重新选择关联组织");
            }
        }

        Customer  cu = new Customer();
        cu.setGuid(customer.getGuid());
        cu.setAllname(customer.getName());
        List<Customer> customers = this.customerService.selectCustomerList(cu);
        if(customers.size() > 0){
            return AjaxResult.error("该客户名称已经存在");
        }
        return null;
    }

    /**
     * 删除客户管理
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(customerService.deleteCustomerByIds(guids));
    }

    @GetMapping(value = "/getCustomerList")
    public AjaxResult getCustomerList(Customer customer)
    {
        List<Customer> list = customerService.selectCustomerList(customer);
        return AjaxResult.success(list);
    }

    /**
     * 通过部门，业务functionType获取客户
     * @return
     */
    @GetMapping(value = "/getCustomers")
    public AjaxResult getCustomers(String departId, String functionType)
    {
        List<Customer> list = customerService.getCustomers(departId, functionType);
        return AjaxResult.success(list);
    }


    /**
     * 发起
     */
    @Log(title = "发起", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Customer customer, String fileIds)
    {
        return customerService.save(customer, fileIds);
    }

    @Log(title = "通用审批", businessType = BusinessType.OTHER)
    @PostMapping("/confirm")
    public AjaxResult confirm(Long taskid,Long id,String opinion,@RequestParam(value="fileIds",required = false)String imgids){
        return customerService.confirm(id, imgids, opinion, taskid);
    }


    @Log(title = "审批", businessType = BusinessType.OTHER)
    @PostMapping("/approval")
    public AjaxResult approval(Long taskid,Long id,String opinion,@RequestParam(value="fileIds",required = false)String imgids){
        return customerService.approval(id, imgids, opinion, taskid);
    }

    @Log(title = "拒绝", businessType = BusinessType.OTHER)
    @PostMapping("/refuse")
    public AjaxResult refuse(Long taskid,Long id,String opinion,String fileIds,Long refuseNodeid,Long backUserid){
        return customerService.refuse(id, fileIds, opinion, taskid,refuseNodeid,backUserid);
    }

    @Log(title = "客户禁用", businessType = BusinessType.OTHER)
    @PostMapping("/unable")
    public AjaxResult unable(Long id){
        return customerService.unable(id);
    }

    /**
     客户审批结束之前可以删除，
     */
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(Long id) {
        return customerService.del(id);
    }

    /**
     *  预收款详情
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:prebalancedetail')")
    @GetMapping(value = "/prebalancedetail")
    public AjaxResult prebalancedetail(Long id)
    {
        return customerService.prebalancedetail(id);
    }
}
